from typing import Annotated
from fastapi import APIRouter, Header
from app.service.common_service import (
    check_token, get_user_nickname, set_user_nickname, get_user_info,
    generate_game, join_game, search_pets
)
import traceback


common_router = APIRouter(
    prefix="/common",
    tags=["common"],
    responses={404: {"description": "Not found"}},
)

"""
检查token，token无效则返回新token
"""
@common_router.get("/checkToken")
async def c_token(token: Annotated[str | None, Header()] = None):
    return check_token(token)

"""
获取用户昵称
"""
@common_router.get("/getUserNickname")
async def g_user_nickname(token: Annotated[str | None, Header()] = None):
    return get_user_nickname(token)

"""
设置用户昵称
"""
@common_router.get("/setUserNickname")
async def s_user_nickname(nickname: str, token: Annotated[str | None, Header()] = None):
    return set_user_nickname(token, nickname)

"""
获取用户信息
"""
@common_router.get("/getUserInfo")
async def g_user_info(token: Annotated[str | None, Header()] = None):
    return get_user_info(token)

"""
创建房间
"""
@common_router.post("/generateGame")
async def g_game(options: dict, token: Annotated[str | None, Header()] = None):
    return generate_game(token, options)

"""
加入房间
"""
@common_router.post("/joinGame")
async def j_game(options: dict, token: Annotated[str | None, Header()] = None):
    return await join_game(token, options)

"""
搜索精灵
"""
@common_router.get("/searchPets")
async def s_pets(search_text: str, page_no: int=1, page_size: int=30):
    return await search_pets(search_text, page_no, page_size)
